<template>
  <div class="container">
    <div class="btnList">
      <el-date-picker
        v-model="erpCenter.date"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-check"
        @click="pull()"
        >拉取ERP</el-button
      >
    </div>
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.searchCode"
        placeholder="请输入店铺或ID"
        clearable
      ></el-input>
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.erpCode"
        placeholder="退货单编号"
        clearable
      ></el-input>

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend">仓库</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>

      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="orgname"
        label="店铺名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="returncode"
        label="退货单单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="returnTimeMsg"
        label="退货时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="examineTimeMsg"
        label="审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="primary"
            v-if="scope.row.status == 1"
            icon="el-icon-edit-outline"
            @click="confirms(scope)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 详情Dailog -->
    <salesReturnDialog :dialog="dialog"></salesReturnDialog>
    <!-- 审核Dailog -->
    <audit :dialogs="dialogs" @upData="getData"></audit>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import { parseTime, getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import SalesReturnDialog from "@/components/salesReturn/salesReturnDialog";
import Audit from "@/components/salesReturn/audit";
import Warehouseselect from "@/components/commodity/warehouseselect";
import { erpCommodityList } from "@/api/commodity/commodityList";
import { refundList, getErprefund } from "@/api/salesReturn/salesReturnList";
export default {
  components: {
    pagination: Pagination,
    salesReturnDialog: SalesReturnDialog,
    audit: Audit,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        searchCode: "",
        erpCode: "",
        status: "",
        start_time: "",
        end_time: "",
      },

      options: [
        {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "未审核",
        },
        {
          value: 2,
          label: "已审核",
        },
      ],
      erpCenter: {
        date: new Date(),
      },
      //dialog详情信息
      dialog: {
        centerDialogVisible: false,
        id: "",
      },
      //dialog审核信息
      dialogs: {
        centerDialogVisible: false,
        id: "",
        auditId: "",
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      warehouseList: {
        name: "",
        code: "",
      },
      date: [],
    };
  },

  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    console.log(this.date);
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        warehouse_code: this.warehouseList.code,
        orgid: this.search.searchCode,
        status: this.search.status,
        code: this.search.erpCode,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
      };
      refundList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.data.forEach((res) => {
              res.returnTimeMsg = parseTime(res.add_time);
              res.examineTimeMsg = parseTime(res.examine_time);
              switch (res.status) {
                case 0:
                  res.statusMsg = "全部";
                  break;
                case 1:
                  res.statusMsg = "未审核";
                  break;
                case 2:
                  res.statusMsg = "已审核";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //详情
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.returnid;
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        searchCode: "",
        erpCode: "",
        status: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
      };
      this.erpCenter = {
        date: new Date(),
      };
      this.warehouseList.name = "";
      this.warehouseList.code = "";
    },

    //审核
    confirms(scope) {
      this.dialogs.centerDialogVisible = true;
      this.dialogs.id = scope.row.returnid;
      this.dialogs.auditId = scope.row.id;
    },

    //拉取erp
    pull(scope) {
      let data = {
        date: parseTime(this.erpCenter.date).split(" ")[0],
      };
      this.$confirm("是否拉取ERP?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getErprefund(data)
            .then((res) => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消拉取ERP",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>